//
// Description: 443. 压缩字符串
// Created by Loading on 2021/8/21.
//

#include <bits/stdc++.h>

using namespace std;

int compress(vector<char> &chars) {
    int n = chars.size();
    if (n == 1) {
        return 1;
    }

    int len = 0, left = 0;
    for (int i = 0; i < n; i++) {
        /* 向后判断是否相等可以防止漏掉最后一个字符的状态 */
        if (i == n - 1 || chars[i] != chars[i + 1]) {
            chars[len++] = chars[i];
            int num = i - left + 1;
            if (num > 1) {
                int index = len;
                while (num > 0) {
                    chars[len++] = num % 10 + '0';
                    num /= 10;
                }
                reverse(&chars[index], &chars[len]);
            }
            left = i + 1;
        }
    }
    return len;
}

int main() {
    vector<char> chars = {'a', 'a', 'b', 'b', 'c'};
    int len = compress(chars);
    cout << len << endl;
    for (int i = 0; i < len; ++i) {
        cout << chars[i];
    }
    cout << endl;
    return 0;
}